﻿<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>矢量图层要素添加</title>
    <script include="jquery" src="./static/libs/include-lib-local.js"></script>
    <script include="mapboxgl,transform" src="./static/libs/include-mapboxgl-local.js"></script>
    <link href="./static/demo/mapboxgl/example/style.css" rel="stylesheet" type="text/css" />
    <style>
        .mapboxgl-ctrl-icon {
            background-position: 50% 50%;
            background-repeat: no-repeat;
            background-size: 60%;
        }

        .mapboxgl-ctrl-icon.mapboxgl-ctrl-add {
            background-image: url('data:image/svg+xml;utf8,<svg t="1594093104250" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="4905"><path d="M789.994 771.154L459.266 670.616l295.152-343.032-380.88 318.478L64.28 553.82l896.008-427.218-170.294 644.552z m-341.168 127.42v-177.38l108.868 30.812-108.868 146.568z" p-id="4906"></path></svg>');
        }

        .mapboxgl-ctrl-icon.mapboxgl-ctrl-update {
            background-image: url('data:image/svg+xml;utf8,<svg t="1594090532673" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="1150"><path d="M512 269.922592l0 121.038704 161.384597-161.384597L512 68.191078l0 121.038704c-178.330548 0-322.770218 144.43967-322.770218 322.770218 0 63.343677 18.559705 122.047684 50.02944 171.874509l58.905598-58.905598c-17.953907-33.689287-28.242228-72.018244-28.242228-112.969934C269.922592 378.251833 378.251833 269.922592 512 269.922592zM784.740778 340.124468l-58.905598 58.905598c17.953907 33.689287 28.242228 72.018244 28.242228 112.969934 0 133.748167-108.329241 242.077408-242.077408 242.077408L512 633.038704 350.614379 794.423301l161.384597 161.384597L511.998977 834.770218c178.330548 0 322.770218-144.43967 322.770218-322.770218C834.770218 448.656323 816.210513 389.952316 784.740778 340.124468z" p-id="1151"></path></svg>');
        }

        .mapboxgl-ctrl-icon.mapboxgl-ctrl-delete {
            background-image: url('data:image/svg+xml;utf8,<svg t="1594092749945" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="4754"><path d="M517.59 21.609c-100.299 0-181.703 79.514-185.167 179.34H98.603c-25.979 0-47.235 21.099-47.235 47.236 0 25.98 21.099 47.237 47.236 47.237h52.117v528.416c0 99.196 67.233 180.285 150.37 180.285h423.55c82.98 0 150.37-80.616 150.37-180.285V295.737h47.236c25.98 0 47.236-21.1 47.236-47.237 0-25.98-21.099-47.236-47.236-47.236H702.441C699.449 101.124 617.888 21.61 517.59 21.61z m-96.677 179.34c3.464-51.172 45.19-90.85 96.834-90.85s93.37 39.835 96.362 90.85H420.913z m-119.98 714.842c-29.444 0-61.88-37.789-61.88-91.953V295.737h547.311V824.31c0 54.007-32.436 91.954-61.88 91.954H300.933v-0.473z m0 0" p-id="4755"></path><path d="M364.387 802.267c21.57 0 39.363-21.571 39.363-48.653V476.022c0-27.082-17.635-48.654-39.363-48.654-21.572 0-39.364 21.572-39.364 48.654v277.592c0 26.924 17.32 48.653 39.364 48.653z m142.496 0c21.571 0 39.363-21.571 39.363-48.653V476.022c0-27.082-17.635-48.654-39.363-48.654-21.571 0-39.364 21.572-39.364 48.654v277.592c0 26.924 17.793 48.653 39.364 48.653z m149.896 0c21.571 0 39.364-21.571 39.364-48.653V476.022c0-27.082-17.635-48.654-39.364-48.654-21.571 0-39.363 21.572-39.363 48.654v277.592c0 26.924 17.162 48.653 39.363 48.653z m0 0" p-id="4756"></path></svg>');
        }
    </style>
    <script type="text/javascript">
        //使用严格模式
        "use strict";
        //定义地图和矢量图层
        var map, vectorLayer, guid;
        //添加要素的FID，由mapgis后台生成，前端无法传入
        var featureIds = ''
        var { protocol, ip, port } = window.webclient;

        /** 地图初始化
         */
        function init() {
            guid = (new Date()).getTime().toString();
            //地图容器
            map = new mapboxgl.Map({
                container: 'map',
                crs: 'EPSG:4326',
                minZoom: 3,
                zoom: 6,
                center: [112.247175, 30.152892]
            });
            var navigationControl = new mapboxgl.NavigationControl();
            map.addControl(navigationControl, 'top-left');

            //创建矢量图层
            vectorLayer = new mapboxgl.Zondy.Map.MapVectorLayer(["gdbp://MapGisLocal/ClientTheme/ds/epsg4326/sfcls/湖北省市级区划2", "gdbp://MapGisLocal/ClientTheme/ds/epsg4326/sfcls/点编辑"], {
                //IP地址
                ip: `${ip}`,
                //端口号
                port: `${port}`,
                //只显示一个图层,不平铺显示
                noWrap: true,
                //添加guid，确保图层从IGS中加载，不读取缓存文件
                guid: guid
            });
            vectorLayer.addToMap(map);
            //注册鼠标移动事件
            map.on('mousemove', function (e) {
                document.getElementById('mouse-position').innerHTML = "经度：" + e.lngLat.lng.toFixed(2) + "，纬度：" + e.lngLat.lat.toFixed(2);
            });
            var html = '<div class="mapboxgl-ctrl mapboxgl-ctrl-group">' +
                '<button class="mapboxgl-ctrl-icon mapboxgl-ctrl-add" type="button" title="矢量要素添加" onclick="addFeature()" aria-label="add"></button>' +
                '<button class="mapboxgl-ctrl-icon mapboxgl-ctrl-update" type="button" title="更新添加的要素" onclick="updateFeature()" aria-label="update"></button>' +
                '<button class="mapboxgl-ctrl-icon mapboxgl-ctrl-delete" type="button" title="删除添加的要素" onclick="deleteFeature()" aria-label="delete"></button>' +
                '</div>';
            $(".mapboxgl-ctrl-top-right").append(html);
        }

        /** 创建一个新要素
         */
        function newFeature(color, fid) {//fid只有在更新要素的时候才会生效
            //创建一个点形状，描述点形状的几何信息
            var gpoint = new Zondy.Object.GPoint(112.35, 31.36);
            //设置当前点要素的几何信息
            var fGeom = new Zondy.Object.FeatureGeometry({ PntGeom: [gpoint] });
            //描述点要素的符号参数信息
            var pointInfo = new Zondy.Object.CPointInfo({
                Angle: 0, Color: color, Space: 0, SymHeight: 600, SymID: 30, SymWidth: 600
            });
            //设置当前点要素的图形参数信息
            var webGraphicInfo = new Zondy.Object.WebGraphicsInfo({
                InfoType: 1, PntInfo: pointInfo
            });
            //设置添加点要素的属性信息
            var attValue = ["1", "市政协", "", "", "江岸区", "0", "0"];
            //创建一个要素
            var feature = new Zondy.Object.Feature({
                fGeom: fGeom, GraphicInfo: webGraphicInfo, AttValue: attValue
            });
            //设置要素为点要素
            feature.setFType(1);
            //设置更新要素的FID
            if (fid !== undefined && fid !== null) {
                feature.setFID(fid);
            }
            //创建一个要素数据集
            var featureSet = new Zondy.Object.FeatureSet();
            featureSet.clear();
            //设置属性结构
            var cAttStruct = new Zondy.Object.CAttStruct({
                FldNumber: 7,
                FldName: ["ID", "名称", "地址", "图片", "城区", "LayerID", "mpLayer"],
                FldType: ["long", "string", "string", "string", "string", "long", "long"],
                FldAlias: [null, null, null, null, null, null, null]
            });
            featureSet.AttStruct = cAttStruct;
            //添加要素到要素数据集
            featureSet.addFeature(feature);
            return featureSet
        }

        /** 添加点要素
         */
        function addFeature() {
            //显示进度条
            startPressBar();
            var featureSet = newFeature(3, null)
            //创建一个编辑服务类
            var editService = new Zondy.Service.EditLayerFeature("gdbp://MapGisLocal/ClientTheme/ds/epsg4326/sfcls/点编辑",
                { ip: `${ip}`, port: `${port}` });
            //执行添加点要素功能,OnSuccess为回调函数
            editService.add(featureSet, addSuccess);
        }

        /** 添加点要素回调函数
         *  @param {json对象} rlt 获取结果对象
         */
        function addSuccess(rlt) {
            //停止进度条
            stopPressBar();
            var result = rlt;
            if (result) {
                //刷新图层
                vectorLayer.refreshMap(guid);
            } else {
                alert("添加点要素失败！");
            }
        }

        /** 查询新添加的要素信息
         */
        async function queryFeatures() {
            //初始化查询结构对象，设置查询结构包含几何信息
            var queryStruct = new Zondy.Service.QueryFeatureStruct();
            //是否包含几何图形信息
            queryStruct.IncludeGeometry = false;
            //是否包含属性信息
            queryStruct.IncludeAttribute = true;
            //是否包含图形显示参数
            queryStruct.IncludeWebGraphic = false;
            //实例化查询参数对象
            var queryParam = new Zondy.Service.QueryByLayerParameter("gdbp://MapGisLocal/ClientTheme/ds/epsg4326/sfcls/点编辑", {
                resultFormat: "json",
                struct: queryStruct
            });
            //设置查询要素数目
            queryParam.recordNumber = 1000;
            //设置属性条件
            queryParam.where = "名称='市政协'";
            //实例化地图文档查询服务对象
            var queryService = new Zondy.Service.QueryLayerFeature(queryParam, {
                ip: `${ip}`,
                port: `${port}`
            });
            //执行查询操作，querySuccess为查询回调函数
            //queryService.query(querySuccess);
            const promise = new Promise(resolve => {
                queryService.query((res) => {
                    if (!res || !res.SFEleArray || res.SFEleArray.length <= 0) {
                        resolve(null)
                    } else {
                        // 获取属性值，还需知道res.AttStruct，才能将值与属性名对应起来
                        resolve(res)
                    }
                })
            })
            return promise.then((res) => {
                return res
            })
        }

        /** 查询成功回调函数
         *  @param {json对象} a 获取结果对象
         */
        async function querySuccess() {
            var a = await queryFeatures()
            if (a.SFEleArray != null) {
                if (a.SFEleArray.length === 0) {
                    alert("查询结果为空");
                } else {
                    featureIds = ''
                    for (var i = 0; i < a.SFEleArray.length; i++) {
                        var obj = a.SFEleArray[i];
                        featureIds += obj.FID
                        if (i < a.SFEleArray.length - 1) {
                            featureIds += ','
                        }
                    }
                }
            } else {
                alert("当前地图未查到要素，请先运行对应的要素添加示例！");
            }
        }

        /** 更新新添加的要素
         */
        async function updateFeature() {
            //更新添加的要素前，先查询出添加要素的FID
            await querySuccess();
            var fid
            if (featureIds.indexOf(',') > -1) {
                var ids = featureIds.split(',')
                fid = ids[ids.length - 1]
            } else {
                fid = featureIds
            }
            //显示进度条
            startPressBar();
            var featureSet = newFeature(12, fid)
            //创建一个编辑服务类
            var editService = new Zondy.Service.EditLayerFeature("gdbp://MapGisLocal/ClientTheme/ds/epsg4326/sfcls/点编辑", {
                ip: `${ip}`,
                port: `${port}`
            });
            //更新所选要素，UpdateSuccess为回调函数
            editService.update(featureSet, UpdateSuccess);
        }

        /** 修改点要素回调函数
         *  @param {json对象} rlt 获取结果对象
         */
        function UpdateSuccess(rlt) {
            //停止进度条
            stopPressBar();
            var result = rlt;
            if (result) {
                //刷新图层
                vectorLayer.refreshMap(guid);
            } else {
                alert("修改点要素失败！");
            }
        }

        /** 删除新添加的要素
         */
        async function deleteFeature() {
            //删除添加的要素前，先查询出添加要素的FID
            await querySuccess();
            //显示进度条
            startPressBar();
            //执行删除要素操作
            var deleteService = new Zondy.Service.EditLayerFeature("gdbp://MapGisLocal/ClientTheme/ds/epsg4326/sfcls/点编辑", {
                ip: `${ip}`,
                port: `${port}`
            });
            //删除所选要素，featureIds为要素id，DeleteSuccess为回调函数
            deleteService.deletes(featureIds, deleteSuccess);
        }

        /** 删除点要素回调函数
         *  @param {json对象} rlt 获取结果对象
         */
        function deleteSuccess(rlt) {
            //停止进度条
            stopPressBar();
            var result = rlt;
            if (result) {
                //刷新图层
                vectorLayer.refreshMap(guid);
            } else {
                alert("删除点要素失败！");
            }
        }

        /** 开始进度条动画
         */
        function startPressBar() {
            document.getElementById('preview').style.display = "";
        }

        /** 停止进度条动画
         */
        function stopPressBar() {
            document.getElementById('preview').style.display = "none";
        }
    </script>
</head>

<body onload="init();">
    <div id="preview"
        style="display: none; text-align: center; padding-top: 250px; font-weight: bold;position: absolute;background: rgba(3, 3, 3, 0.1);color: #fff;float: left; width: 100%;height: 100%;-webkit-border-radius: 10px;-moz-border-radius: 10px;border-radius: 10px;z-index: 20000;">
        <img src="./static/assets/graphic-image/39-1.gif" alt='' /><br />
        <br />
        <span>正在操作，请稍候</span>
    </div>
    <div id="map" style="width: 100%; height:700px;">
        <div id="mouse-position">
        </div>
    </div>
</body>

</html>